﻿using CPPEI.ETL.DataServiceInterface;
using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using SER = CPPEI.ETL.Domain.Services;
using VIW = CPPEI.ETL.Domain.Views;

namespace CPPEI.ETL.WebApi
{
    [CPPEI.WebApi.AuthGetAction("ETL", "GetDataTable", "1.0")]
    public class GetDataTableAction : BaseAction<GetDataTableParams, GetDataTableResult>
    {
        protected override ActionResult<GetDataTableResult> OnExecute(ActionContext context,
            SysParameters sysParameters,
            GetDataTableParams opParams)
        {
            SER.DtTableService service = new SER.DtTableService();

            ServiceResult<VIW.VDtTableDetail> serResult = service.GetVTable(opParams.DataTableId);
            if (serResult.IsSuccess)
            {
                GetDataTableResult actResult = new GetDataTableResult();
                actResult.Id = serResult.Data.DataTable.Id;
                actResult.Name = serResult.Data.DataTable.Name;
                actResult.CreatedTime = serResult.Data.DataTable.CreatedTime;
                actResult.IsRawDt = serResult.Data.DataTable.IsRawDt;
                actResult.DataSources = new List<GetDataTableResult.DataSource>();

                for (int index = 0; index < serResult.Data.DataSources.Count; index++)
                {
                    GetDataTableResult.DataSource source = new GetDataTableResult.DataSource();
                    source.DataSourceId = serResult.Data.DataSources[index].DataSource.Id;
                    source.DataServiceId = serResult.Data.DataSources[index].DataSource.DtSerId;
                    source.DataSourceName = serResult.Data.DataSources[index].DataSource.Name;

                    source.Columns = new List<GetDataTableResult.Column>();
                    actResult.DataSources.Add(source);

                    VIW.VDtTableDetail.DataSourceMapping mapping = serResult.Data.DataSources[index];

                    for (int rowIndex = 0; rowIndex < mapping.Columns.Count; rowIndex++)
                    {
                        GetDataTableResult.Column column = new GetDataTableResult.Column();

                        column.Id = mapping.Columns[rowIndex].Id;
                        column.Name = mapping.Columns[rowIndex].Name;

                        column.CreatedTime = mapping.Columns[rowIndex].CreatedTime;

                        column.Nullable = mapping.Columns[rowIndex].Nullable;

                        column.Length = mapping.Columns[rowIndex].Length;

                        column.Type = mapping.Columns[rowIndex].Type;


                        column.MappingColumnType = mapping.Columns[rowIndex].MappingColumnType;

                        if (!string.IsNullOrEmpty(mapping.Columns[rowIndex].MappingColumnName))
                        {
                            column.MappingColumnName = mapping.Columns[rowIndex].MappingColumnName;
                        }
                        else
                        {
                            column.MappingColumnName = string.Empty;
                        }

                        if (mapping.Columns[rowIndex].MappingColumnId != null)
                        {
                            column.MappingColumnId = mapping.Columns[rowIndex].MappingColumnId.Value;
                        }

                        source.Columns.Add(column);

                        column.IsDataIdentity = false;
                        for (int diIndex = 0; diIndex < mapping.DataIdentities.Count; diIndex++)
                        {
                            if (mapping.DataIdentities[diIndex].DtSourceColumnId == column.MappingColumnId)
                            {
                                column.IsDataIdentity = true;
                                mapping.DataIdentities.RemoveAt(diIndex);
                                break;
                            }
                        }
                    }
                }


                return new ActionResult<GetDataTableResult>(actResult);
            }
            else
            {
                return new ActionResult<GetDataTableResult>(serResult.IsSuccess, serResult.ErrorCode, serResult.Message, null);
            }
        }
    }

    public class GetDataTableParams
    {
        [Parameter]
        public Guid DataTableId { get; set; }
    }

    public class GetDataTableResult
    {
        public Guid Id { get; set; }
        public string Name { get; set; }


        public DateTime CreatedTime { get; set; }

        public bool IsRawDt { get; set; }

        public List<DataSource> DataSources { get; set; }

        public class DataSource
        {
            public string DataSourceName { get; set; }

            public Guid DataSourceId { get; set; }

            public Guid DataServiceId { get; set; }

            public List<Column> Columns { get; set; }
        }

        public class Column
        {
            public Guid Id { get; set; }
            public string Name { get; set; }

            public DateTime CreatedTime { get; set; }

            public bool Nullable { get; set; }

            public int Length { get; set; }

            public string Type { get; set; }

            public string MappingColumnType { get; set; }

            public string MappingColumnName { get; set; }

            public Guid MappingColumnId { get; set; }

            /// <summary>
            /// 是否是数据标识字段
            /// </summary>
            public bool IsDataIdentity{get;set;}
        }
    }
}
